package com.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.core.AbstractMessageSendingTemplate;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.messaging.simp.user.SimpUser;
import org.springframework.messaging.simp.user.SimpUserRegistry;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
public class TestController {
    @Autowired
    SimpUserRegistry userRegistry;

    @Autowired
    SimpMessagingTemplate simpMessagingTemplate;
    /**
     todo
     send user info ,to subpub user,
     */
    public void sendUser(){
        userRegistry.getUser("");
        simpMessagingTemplate.convertAndSend("");
        // ..
    }


    @MessageMapping("/queue")
    public String subscribeForQueue(String msg){
        return "msg";
    }

    @ResponseBody
    @PostMapping("currentAdminSession")
    public List<String> getAdminSession(){
        /**
         todo
         i get the sessionId that is belong to admin when it is online .
         String name = jdbcRepository.getAdminName();
         */
        SimpUser user = userRegistry.getUser("");
        List<String> collect = user.getSessions().stream().map(simpSession -> simpSession.getId()).collect(Collectors.toList());
        log.info("collect:{}",collect.toString());
        return collect;
    }
    @ResponseBody
    @PostMapping("currentUserSession")
    public List<String> getUserSession(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        SimpUser user = userRegistry.getUser(name);
        List<String> collect = user.getSessions().stream().map(simpSession -> simpSession.getId()).collect(Collectors.toList());
        log.info("collect:{}",collect.toString());
        return collect;
    }

    @SendToUser("/queue/foo")
    @MessageMapping("/trade")
    public String handleTradeMessage(String info){
        log.info("{}",info);
        return "handleTradeMessage";
    }
    @SendToUser("/queue/fruit")
    @MessageMapping("/fruit")
    public String handleFruitMessage(String info){
        log.info("{}",info);
        return "handleFruitMessage";
    }

    @MessageMapping("/info")
    public String handleMessage(String info){
        log.info("{}",info);
        return "bar-msg";
    }


}
